#include "phoneloginwidget.h"
#include "loginwidget.h"
#include "debug.h"

PhoneLoginWidget::PhoneLoginWidget(QWidget *parent)
    :QWidget(parent), isLoginMode(true)
{

    //设置基本属性
    this->setFixedSize(400,400);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/image/logo.png"));
    this->setStyleSheet("QWidget{background-color:rgb(255, 255, 255);}");
    this->setAttribute(Qt::WA_DeleteOnClose);
    timer = new QTimer(this);

    tipLabel = new QLabel();
    tipLabel->setText("登录");
    tipLabel->setAlignment(Qt::AlignCenter);
    tipLabel->setFixedHeight(50);
    tipLabel->setStyleSheet("QLabel{font-size:40px;}");

    phoneEdit = new QLineEdit();
    phoneEdit->setPlaceholderText("输入手机号");
    phoneEdit->setFixedHeight(40);
    phoneEdit->setStyleSheet("QLineEdit{background-color:rgb(235, 235, 235); border:none; border-radius:5px; padding-left:10px; font-size: 20px;}");

    verifyCodeEdit = new QLineEdit();
    verifyCodeEdit->setPlaceholderText("输入验证码");
    verifyCodeEdit->setFixedHeight(40);
    verifyCodeEdit->setStyleSheet("QLineEdit{background-color:rgb(235, 235, 235); border:none; border-radius:5px; padding-left:10px; font-size: 20px;}");

    //一定要设置验证码Edit逻辑为尽可能水平扩展, 因为水平还有一个按钮
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    sendVerifyCodeBtn = new QPushButton();
    sendVerifyCodeBtn->setText("发送验证码");
    sendVerifyCodeBtn->setFixedHeight(40);
    sendVerifyCodeBtn->setStyleSheet("QPushButton { border: none; border-radius:5px;} QPushButton:pressed {background-color: rgb(235, 235, 235);}");

    submitBtn = new QPushButton();
    submitBtn->setText("登录");
    submitBtn->setFixedHeight(40);
    submitBtn->setStyleSheet("QPushButton{background-color:rgb(44, 182, 61); border:none; border-radius:5px;}");

    userModeBtn = new QPushButton();
    userModeBtn->setText("用户名登录");
    userModeBtn->setFixedHeight(40);
    userModeBtn->setStyleSheet("QPushButton { border: none; border-radius:5px;} QPushButton:pressed {background-color: rgb(235, 235, 235);}");

    switchModeBtn = new QPushButton();
    switchModeBtn->setText("注册");
    switchModeBtn->setFixedHeight(40);
    switchModeBtn->setStyleSheet("QPushButton { border: none; border-radius:5px;} QPushButton:pressed {background-color: rgb(235, 235, 235);}");
    /// 切换窗口
    connect(userModeBtn,&QPushButton::clicked,this, &PhoneLoginWidget::switchToUsername);
    /// 切换当前窗口注册与登录
    connect(switchModeBtn,&QPushButton::clicked,this, &PhoneLoginWidget::switchMode);
    /// 发送验证码按钮
    connect(sendVerifyCodeBtn,&QPushButton::clicked,this, &PhoneLoginWidget::clickSendVerifyCodeBtn);

    //添加各个控件到布局管理器中
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(50,20,50,20);
    layout->setSpacing(10);
    layout->addWidget(tipLabel,0,0,1,5);
    layout->addWidget(phoneEdit,1,0,1,5);
    layout->addWidget(verifyCodeEdit,2,0,1,4);
    layout->addWidget(sendVerifyCodeBtn,2,4,1,1);
    layout->addWidget(submitBtn,3,0,1,5);
    layout->addWidget(userModeBtn,4,0,1,1);
    layout->addWidget(switchModeBtn,4,4,1,1);
    this->setLayout(layout);
}

void PhoneLoginWidget::switchMode()
{
    if(isLoginMode)
    {
        tipLabel->setText("注册");
        submitBtn->setText("注册");
        sendVerifyCodeBtn->setText("手机号注册");
        switchModeBtn->setText("登录");
    }
    else
    {
        tipLabel->setText("登录");
        submitBtn->setText("登录");
        sendVerifyCodeBtn->setText("手机号登录");
        switchModeBtn->setText("注册");
    }

    isLoginMode = !isLoginMode;
}

void PhoneLoginWidget::switchToUsername()
{
    LOG() << " void PhoneLoginWidget::switchToUsername()";
    LoginWidget* lw = new LoginWidget();
    lw->show();
    this->close();

}

void PhoneLoginWidget::clickSendVerifyCodeBtn()
{
    QString phone = phoneEdit->text();
    // 如果手机号为空
    if(phone.isEmpty())
    {
        Toast::showMessage("请输入手机号!");
        return;
    }

    this->currentPhone = phone;

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter,&model::DataCenter::getVerifyCodeDone,this,&PhoneLoginWidget::sendVerifyCodeDone,Qt::UniqueConnection);
    dataCenter->getVerifyCodeAsync(phone);
}

void PhoneLoginWidget::sendVerifyCodeDone()
{
    Toast::showMessage("验证码已发送, 请注意查收");

    sendVerifyCodeBtn->setEnabled(false);
    count = 60;
    connect(timer,&QTimer::timeout,this,&PhoneLoginWidget::countDown,Qt::UniqueConnection);
    timer->start(1000);
}

void PhoneLoginWidget::countDown()
{
    if(count >= 1)
    {
        sendVerifyCodeBtn->setText(QString::number(count) + "s");
        --count;
    }
    else
    {
        sendVerifyCodeBtn->setText("发送验证码");
        sendVerifyCodeBtn->setEnabled(true);
        timer->stop();
        count = 60;
    }
}

void PhoneLoginWidget::clickSubmitBtn()
{
    QString verifyCode = verifyCodeEdit->text();

    if(verifyCode.isEmpty())
    {
        Toast::showMessage("请输入验证码!");
        return;
    }

    model::DataCenter* dataCenter = model::DataCenter::getInstance();

    if(isLoginMode)
    {
        connect(dataCenter,&model::DataCenter::phoneLoginDone,this,&PhoneLoginWidget::phoneLoginDone);
        dataCenter->phoneLoginAsync(currentPhone,verifyCode);
    }
    else
    {
        connect(dataCenter,&model::DataCenter::phoneRegisterDone,this,&PhoneLoginWidget::phoneRegisterDone);
        dataCenter->phoneRegisterAsync(currentPhone,verifyCode);
    }
}

void PhoneLoginWidget::phoneLoginDone(bool ok, const QString &reason)
{
    if(ok)
    {
        Toast::showMessage("登录成功!");
        MainWidget* w = MainWidget::getInstance();
        w->show();
        this->close();
    }
    else
    {
        Toast::showMessage("注册失败!, 失败原因: " + reason);
        return;
    }
}

void PhoneLoginWidget::phoneRegisterDone(bool ok, const QString &reason)
{
    if(ok)
    {
        Toast::showMessage("注册成功!");
        switchMode();

        //电话与验证码输入框清空
        phoneEdit->setText("");
        verifyCodeEdit->setText("");

        //更新定时器状态
        timer->stop();
        count = 60;
        sendVerifyCodeBtn->setText("发送验证码");
        sendVerifyCodeBtn->setEnabled(true);
    }
    else
    {
        Toast::showMessage("注册失败!, 失败原因: " + reason);
        return;
    }
}

